<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Content_handle extends MY_Controller
{
    function get_info_list()
    {
        $cid = ( int ) $this->input->get( 'cid' );
        $begin = ( int ) $this->input->get( 'begin' );
        $length = ( int ) $this->input->get( 'length' );
        
        if ($cid < 1)
        {
            $this->rs->err( 2 );
        }
        $begin = $begin < 0 ? 0 : $begin;
        $length = $length < 1 ? 10 : $length;

        $this->load->model( 'content_model' );
        $count = $this->content_model->get_list_count( null, $cid );
        if ($begin > 0 && $begin >= $count)
        {
            $this->rs->err( 4 );
        }
        $list = $this->content_model->get_list( null, $cid, $length, $begin );
        $this->rs->ok( array (
                'record_count' => $count,
                'list' => $list 
        ) );
    }
    function get_list()
    {
        $keywords = trim( $this->input->get( 'keywords' ) );
        $gid = ( int ) $this->input->get( 'gid' );
        $cid = ( int ) $this->input->get( 'cid' );
        $begin = ( int ) $this->input->get( 'begin' );
        $length = ( int ) $this->input->get( 'length' );
        
        if ($gid < 1)
        {
            $this->rs->err( 2 );
        }
        $this->load->model( 'category_model' );
        $this->category_model->load( $gid );
        $category_tree = $this->category_model->get_data();
        if (empty( $category_tree ))
        {
            $this->rs->err( '无法获取分类！' );
        }
        if (! $this->category_model->get_current( $cid ))
        {
            $cid = $category_tree[0][0]['id'];
        }
        $cids = $this->category_model->get_ids( $cid );
        $begin = $begin < 0 ? 0 : $begin;
        $length = $length < 1 ? 10 : $length;
        
        $this->load->model( 'content_model' );
        $count = $this->content_model->get_list_count( $keywords, $cids );
        if ($begin > 0 && $begin >= $count)
        {
            $this->rs->err( 4 );
        }
        $list = $this->content_model->get_list( $keywords, $cids, $length, $begin, true );
        $category_map = $this->category_model->get_map();
        foreach ( $list as &$item )
        {
            $item['category'] = $category_map[$item['cid']]['title'];
            $properties = json_decode( $item['properties'], true );
            if (is_array( $properties ))
            {
                $item += $properties;
            }
        }
        $this->rs->ok( array (
                'category_tree' => $category_tree,
                'cid' => $cid,
                'record_count' => $count,
                'list' => $list 
        ) );
    }
    function get()
    {
        $id = ( int ) $this->input->get( 'id' );
        $this->load->model( 'content_model' );
        if ($content = $this->content_model->get( $id ))
        {
            $properties = json_decode( $content['properties'], true );
            if (is_array( $properties ))
            {
                $content += $properties;
            }
            $this->load->model( 'category_model' );
            $this->category_model->load( $content['gid'] );
            $content['category_tree'] = $this->category_model->get_data();
            $this->rs->ok( $content );
        }
        $this->rs->err( '内容不存在！' );
    }
    function prepare_add()
    {
        $gid = ( int ) $this->input->get( 'gid' );
        if ($gid < 1)
        {
            $this->rs->err( 2 );
        }
        $this->load->model( 'category_model' );
        $this->category_model->load( $gid );
        $category_tree = $this->category_model->get_data();
        if (empty( $category_tree ))
        {
            $this->rs->err( '无法获取分类！' );
        }
        $this->load->model( 'content_model' );
        $max_sort = $this->content_model->max_sort( $gid );
        if ($max_sort === false)
        {
            $this->rs->err( '无法获取最大排序号！' );
        }
        $this->rs->ok( array (
                'category_tree' => $category_tree,
                'sort' => $max_sort + 1 
        ) );
    }
    function modify()
    {
        $this->load->model( 'content_model' );
        $id = ( int ) $this->input->post( 'id' );
        $is_insert = $id > 0 ? false : true;
        
        if (! $is_insert)
        {
            $old_data = $this->content_model->get( $id );
            if (! $old_data)
            {
                $this->rs->err( '内容不存在！' );
            }
        }
        
        $modify_data = array ();
        
        $cid = $this->input->post( 'cid' );
        if ($is_insert || isset( $cid ))
        {
            $this->load->model( 'category_model' );
            $category = $this->category_model->get( $cid );
            if (! $category)
            {
                $this->rs->err( '分类错误！' );
            }
            if (! empty( $category['model'] ) && empty( $category['fid'] ))
            {
                $this->rs->err( '请选择分类！' );
            }
            $modify_data['gid'] = $category['gid'];
            $modify_data['cid'] = $category['id'];
            $modify_data['uri'] = $category['content_uri'];
        }
        
        $deleted = $this->input->post( 'deleted' );
        if ($is_insert || isset( $deleted ))
        {
            $modify_data['deleted'] = $deleted ? 1 : 0;
        }
        
        // flag有4种修改方式
        // 1.根据位修改参数rec、hot、ad重构flag。
        // 2.直接将修改后的flag传回，相应的位修改参数rec、hot、ad就不要了，即后台获取的话值为null。
        // 3.flag传回未修改前的值，如果传了rec、hot、ad表示对对应位需要修改，置0还是置1，由其传回的值确定。
        // 4.与3类似，但flag的旧值不是从前台传回，而是从数据库读取。
        // 注意：
        // checkbox是不能传0回来的，因为checkbox未选中时，直接不传值。
        // 由于rec、hot、ad等置位参数对应有三种状态，因此前端不能直接用checkbox实现。
        // 第1种方式，在功能修改时，一般会将其他位的设置丢掉，而且具有一定隐蔽性，测试时对当前功能不会有什么表象。
        // 第2、3种，也存在恶意丢掉其他位设置的可能。
        // 第4种，虽然要读数据库，牺牲了一点性能，但避免了前三种的缺陷，从程序逻辑严谨性来说必须这样，推荐！
        $flag = $is_insert ? 0 : $old_data['flag'];
        $hot = $this->input->post( 'hot' );
        if (isset( $hot ))
        {
            $flag = $hot ? $flag | (1 << 0) : $flag & ~ (1 << 0);
        }
        $rec = $this->input->post( 'rec' );
        if (isset( $rec ))
        {
            $flag = $rec ? $flag | (1 << 1) : $flag & ~ (1 << 1);
        }
        $ad = $this->input->post( 'ad' );
        if (isset( $ad ))
        {
            $flag = $ad ? $flag | (1 << 2) : $flag & ~ (1 << 2);
        }
        $modify_data['flag'] = $flag;
        
        $img_cover = $this->input->post( 'img_cover' );
        if (isset( $img_cover ))
        {
            $modify_data['img_cover'] = substr_utf8( $img_cover, 255 );
        }
        
        $title = $this->input->post( 'title' );
        if ($is_insert || isset( $title ))
        {
            if (empty( $title ))
            {
                $this->rs->err( '标题不能为空' );
            }
            $modify_data['title'] = substr_utf8( $title, 255 );
        }
        
        $subtitle = $this->input->post( 'subtitle' );
        if (isset( $subtitle ))
        {
            $modify_data['subtitle'] = substr_utf8( $subtitle, 255 );
        }
        
        $summary = $this->input->post( 'summary' );
        if (isset( $summary ))
        {
            $modify_data['summary'] = substr_utf8( $summary, 255 );
        }
        
        $en_summary = $this->input->post( 'en_summary' );
        if (isset( $en_summary ))
        {
            $modify_data['en_summary'] = substr_utf8( $en_summary, 255 );
        }
        
        $price = $this->input->post( 'price' );
        if (isset( $price ))
        {
            if (! preg_match( "/^\d{1,8}(\.\d{1,2})?$/", $price ))
            {
                $this->rs->err( '价格参数错误！' );
            }
            $modify_data['price'] = $price;
        }
        
        $price_old = $this->input->post( 'price_old' );
        if (isset( $price_old ))
        {
            if (! preg_match( "/^\d{1,8}(\.\d{1,2})?$/", $price_old ))
            {
                $this->rs->err( '原价参数错误！' );
            }
            $modify_data['price_old'] = $price_old;
        }
        
        $file_description = $this->input->post( 'file_description' );
        if (isset( $file_description ))
        {
            $modify_data['file_description'] = substr_utf8( $file_description, 255 );
        }
        
        $file_type = $this->input->post( 'file_type' );
        if (isset( $file_type ))
        {
            $modify_data['file_type'] = substr_utf8( $file_type, 50 );
        }
        
        $file_size = $this->input->post( 'file_size' );
        if (isset( $file_size ))
        {
            $modify_data['file_size'] = ( int ) $file_size;
        }
        
        $file_path = $this->input->post( 'file_path' );
        if (isset( $file_path ))
        {
            $modify_data['file_path'] = substr_utf8( $file_path, 255 );
        }
        
        $content = $this->input->post( 'content' );
        if (isset( $content ))
        {
            $modify_data['content'] = $content;
        }
        
        $en_content = $this->input->post( 'en_content' );
        if (isset( $en_content ))
        {
            $modify_data['en_content'] = $en_content;
        }
        
        // 自定义属性
        if ($is_insert)
        {
            $properties = array ();
        }
        else
        {
            $properties = json_decode( $old_data['properties'], true );
            is_array( $properties ) || $properties = array ();
        }
        $color = $this->input->post( 'color' );
        if (isset( $color ))
        {
            $properties['color'] = $color;
        }
        $modify_data['properties'] = json_encode( $properties, JSON_UNESCAPED_UNICODE );
        
        $head_title = $this->input->post( 'head_title' );
        if (isset( $head_title ))
        {
            $modify_data['head_title'] = substr_utf8( $head_title, 255 );
        }
        
        $en_head_title = $this->input->post( 'en_head_title' );
        if (isset( $en_head_title ))
        {
            $modify_data['en_head_title'] = substr_utf8( $en_head_title, 255 );
        }
        
        $head_keywords = $this->input->post( 'head_keywords' );
        if (isset( $head_keywords ))
        {
            $modify_data['head_keywords'] = substr_utf8( $head_keywords, 255 );
        }
        
        $en_head_keywords = $this->input->post( 'en_head_keywords' );
        if (isset( $en_head_keywords ))
        {
            $modify_data['en_head_keywords'] = substr_utf8( $en_head_keywords, 255 );
        }
        
        $head_description = $this->input->post( 'head_description' );
        if (isset( $head_description ))
        {
            $modify_data['head_description'] = substr_utf8( $head_description, 255 );
        }
        
        $en_head_description = $this->input->post( 'en_head_description' );
        if (isset( $en_head_description ))
        {
            $modify_data['en_head_description'] = substr_utf8( $en_head_description, 255 );
        }
        
        $sort = $this->input->post( 'sort' );
        if ($is_insert || isset( $sort ))
        {
            $modify_data['sort'] = ( int ) $sort;
        }
        
        $enable = $this->input->post( 'enable' );
        if ($is_insert || isset( $enable ))
        {
            $modify_data['enable'] = $enable ? 1 : 0;
        }
        
        if (empty( $modify_data ))
        {
            $this->rs->err( 2 );
        }
        if ($is_insert)
        {
            $modify_data['add_time'] = $_SERVER['REQUEST_TIME'];
            if ($this->content_model->insert( $modify_data ))
            {
                $this->rs->ok();
            }
        }
        else
        {
            if (false !== $this->content_model->update( $modify_data, $id ))
            {
                $this->rs->ok();
            }
        }
        $this->rs->err();
    }
}